import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;



public class parser
{

	public static void main(String args[])
	{

		String city = "PLACE_HOLDER";
		String state = "PLACE_HOLDER";
		String shape = "PLACE_HOLDER";
		String duration = "PLACE_HOLDER";
		String description = "PLACE_HOLDER";
		String date = "PLACE_HOLDER";
		String longitudeFINAL = "PLACE_HOLDER";
		String latitudeFINAL = "PLACE_HOLDER";
		boolean printSomething = false;
		ArrayList<noob> longlat = new ArrayList<noob>();

		try{
			  // Create file 
			  FileWriter fstream4 = new FileWriter("out2.txt", true);
			  BufferedWriter out2 = new BufferedWriter(fstream4);
			
			//read in lat and long of all us cities and store in array
			FileInputStream fstream2 = new FileInputStream("longlat.txt");
			DataInputStream in2 = new DataInputStream(fstream2);
			BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
			String strLine2;
			  
			String longitude ="";
			String latitude ="";
			String cityName = "";
			String stateName = "";
			String concated = "";
			
			System.out.println ("Reading longitude and latitude file...");
			//Read File Line By Line
			int arrayIndex =0;
			while ((strLine2 = br2.readLine()) != null)
			{
				
				//break up line by ','
				int counter2=0;
				for (int i=0; i< strLine2.length(); i++)
				{
					if (strLine2.charAt(i) == (','))
					{
						counter2++;
					}
					
					
					if (counter2 == 1)
					{
						longitude += strLine2.charAt(i+1);
					}
					else if (counter2 == 2)
					{
						latitude += strLine2.charAt(i+1);
					}
					else if (counter2 == 3)
					{
						cityName += strLine2.charAt(i+1);
					}
					else if (counter2 == 4)
					{
						stateName += strLine2.charAt(i+1);
					}

					
				}
				longitude = longitude.substring(0, longitude.length()-1);
				latitude = latitude.substring(0, latitude.length()-1);
				cityName = cityName.substring(0, cityName.length()-1);
				stateName = stateName.substring(0, stateName.length()-1);
				
				concated = longitude + "\t" + latitude + "\t" + cityName + "\t" + stateName;
				
				//System.out.println ("TEST: " + concated);
				
				
				noob theNoob = new noob(stateName, cityName.toLowerCase(), latitude, longitude);
				
				longlat.add(theNoob);
				
				
				
				
				  //write to file
				  try
				  {

						  out2.write(cityName.toLowerCase() + "\t" + stateName + "\t" + longitude + "\t" + latitude + "\n");

				  }
				  catch (Exception ex4)
				  {
					  System.err.println("Error: " + ex4.getMessage());
				  }
				
				concated = "";
				longitude ="";
				latitude ="";
				cityName = "";
				stateName = "";
				
				
			}//while loop for arrayList
			  
			System.out.print ("Done!\n");
			in2.close();
			out2.close();
		}
		catch (Exception ex2)
		{
			System.err.println("Error: " + ex2.getMessage());
		}
		
		
		File directory = new File("./states");
		File files[] = directory.listFiles();
		for (File f : files) {
		   // do whatever you want with each  File f
			System.out.println ( "Opening: " + f.getName() );
			
			String theFilename = "./states/" + f.getName();
		

	try{
		  // Create file 
		  FileWriter fstream3 = new FileWriter("out.txt", true);
		  BufferedWriter out = new BufferedWriter(fstream3);
			
		  // Open the file
		  FileInputStream fstream = new FileInputStream(theFilename);
		  
		  // Get the object of DataInputStream
		  DataInputStream in = new DataInputStream(fstream);
		  BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  String strLine;
		  
		  //Read File Line By Line
		  while ((strLine = br.readLine()) != null)
		  {
			  
			  //get a block of info
			  if (strLine.startsWith("<TR"))
			  {
				  printSomething = true;
				  
				  //get date
				  strLine = br.readLine();
				  
				  if (strLine.contains("FFFFCC"))
				  {
					  int counter =0;
					  
					  for (int i=0; i< strLine.length(); i++)
					  {
						  if (strLine.charAt(i) == ('>'))
						  {
							  counter++;
						  }
						  if (counter == 3)
						  {
							  date += strLine.charAt(i);
						  }
						  
					  }
					  
					  if (date.length() > 2)
						  date = date.substring(1, date.length()-3);
				  }
				  else
				  {
					  int counter =0;
					  
					  for (int i=0; i< strLine.length(); i++)
					  {
						  if (strLine.charAt(i) == ('>'))
						  {
							  counter++;
						  }
						  if (counter == 3)
						  {
							  date += strLine.charAt(i);
						  }
						  
					  }
					  if (date.length() > 2)
						  date = date.substring(1, date.length()-3);
				  }
				  
				  
				  //get city
				  strLine = br.readLine();
				  if (strLine.contains("FFFFCC"))
				  {
					  city = strLine.substring(79, strLine.length());
					  city = city.substring(0, city.length()-5);
					  
					  //substring out (desciprionts in city)
					  if (city.contains("("))
					  {
						  int index =0;
						  
						  for (int i=0; i< city.length(); i++)
						  {
							  if (city.charAt(i) == '(')
							  {
								  index= i;
								  break;
							  }
						  }
						  
						  city = city.substring(0, index);
					  }
					  
					  city=city.trim();
				  }
				  else
				  {
					  city = strLine.substring(60, strLine.length());
					  city = city.substring(0, city.length()-5);
					  
					  //substring out (desciprionts in city)
					  if (city.contains("("))
					  {
						  int index =0;
						  
						  for (int i=0; i< city.length(); i++)
						  {
							  if (city.charAt(i) == '(')
							  {
								  index= i;
								  break;
							  }
						  }
						  
						  city = city.substring(0, index);
					  }
					  
					  city=city.trim();
				  }
				  
				  //get state
				  strLine = br.readLine();
				  if (strLine.contains("FFFFCC"))
				  {
					  state = strLine.substring(79, strLine.length());
					  state = state.substring(0, state.length()-5);
				  }
				  else
				  {
					  state = strLine.substring(60, strLine.length());
					  state = state.substring(0, state.length()-5);
				  }
				  
				  //get shape
				  strLine = br.readLine();
				  if (strLine.contains("FFFFCC"))
				  {
					  shape = strLine.substring(79, strLine.length());
					  shape = shape.substring(0, shape.length()-5);
				  }
				  else
				  {
					  shape = strLine.substring(60, strLine.length());
					  shape = shape.substring(0, shape.length()-5);
				  }
				  
				//get duration
				  strLine = br.readLine();
				  if (strLine.contains("FFFFCC"))
				  {
					  duration = strLine.substring(79, strLine.length());
					  duration = duration.substring(0, duration.length()-5);
				  }
				  else
				  {
					  duration = strLine.substring(60, strLine.length());
					  duration = duration.substring(0, duration.length()-5);
				  }
				  
				//get description
				  strLine = br.readLine();
				  if (strLine.contains("FFFFCC"))
				  {
					  description = strLine.substring(79, strLine.length());
					  description = description.substring(0, description.length()-5);
				  }
				  else
				  {
					  description = strLine.substring(60, strLine.length());
					  description = description.substring(0, description.length()-5);
				  }
				  
			  }
			  else
			  {
				  printSomething = false;
			  }

			  if (!validDate(date))
			  {
				  printSomething = false;
			  }
			  
			  //check if user left an area empty
			  if (city.equals("<BR>"))
				  city = "BLANK";
					  
			  if (state.equals("<BR>"))
				  state = "BLANK";
			  
			  if (shape.equals("<BR>"))
				  shape = "BLANK";
			  
			  if (duration.equals("<BR>"))
				  duration = "BLANK";
			  
			  if (description.equals("<BR>"))
				  description = "BLANK";
			  
			  if (date.equals("<BR>"))
				  date = "BLANK";
				  		  
			  if (city.contains("ZE:11pt"))
				  printSomething = false;
			  
			  
			if (printSomething)
			{
				noob theNoob = new noob(state, city.toLowerCase());
			
			
						 int longLatIndex = longlat.indexOf(theNoob);
						 
						 if (longLatIndex != -1)
						 {
							 longitudeFINAL = ( longlat.get(longLatIndex).getLong());
							 latitudeFINAL = ( longlat.get(longLatIndex).getLat());
						 }
						  
						  
						  
						  String test = date + "\t" +city +"\t" + state + "\t" + shape + "\t" + duration + "\t" + description + "\t" + longitudeFINAL + "\t" + latitudeFINAL;
						  	  
						  //check if test string has <BR> in it.. delete it
						  while (test.contains("<BR>"))
						  {
							  for (int i=0; i<test.length(); i++)
							  {
								  if ((i+3) < test.length() && test.charAt(i) == '<' && test.charAt(i+1) == 'B' && test.charAt(i+2) == 'R' && test.charAt(i+3) == '>')
								  {
									  String part1;
									  String part2;
									  
									  part1 = test.substring(0, i);
									  part2 = test.substring(i+4, test.length());
									  
									  test = part1 + part2;
								  }
							  }
						  }
						  
						  boolean isNope = false;
						  if (latitudeFINAL.equals("NOOOOOOOOOOOOOOOOOOPE"))
							  isNope = true;
						  
				//write to file
			  try
			  {
				  if (!isNope)
					  out.write( test + "\n");
				  
			  }
			  catch (Exception ex)
			  {
				  System.err.println("Error: " + ex.getMessage());
			  }		  
			}
			  
			  
			  
			  
			  date = "";
			  longitudeFINAL = "NOOOOOOOOOOOOOOOOOOPE";
			  latitudeFINAL = "NOOOOOOOOOOOOOOOOOOPE";
			  
		  } //end while
		  
		in.close();
		out.close();
		  
		}
		catch (Exception ex)
		{
			System.err.println("Error: " + ex.getMessage());
		}
	
	
	}//end for loop
		System.out.println ("Done parsing HTML files");
		
	}//main loop
	
	static boolean validDate(String x) {
		// System.out.println("start checking dates");
		boolean valid = false;
		int size = 0;
		String checkDate = "";
		int numCheckDate = 0;
		size = x.length();

		int count = 0;
		for (int i = 0; i < size-2; i++) {
			//System.out.println(x.charAt(i));
			if (x.charAt(i) == '/') {
				count++;
			}
			if (count == 2) {
				checkDate += x.charAt(i + 1);
				checkDate += x.charAt(i + 2);
				
				count = 0;
			}
			
			if ((checkDate.equals("01")) || (checkDate.equals("02"))||(checkDate.equals("03"))||(checkDate.equals("04"))||(checkDate.equals("05"))||
					(checkDate.equals("06"))||(checkDate.equals("07"))||(checkDate.equals("08"))||(checkDate.equals("09"))||(checkDate.equals("10"))) 
			{
				valid = true;
			} else {
				valid = false;
			}

		}

		// System.out.println(checkDate);
		return valid;

	}
}//class loop